package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.InspectionJournalHeaderEntity;
import cn.com.huzo.repository.trade.InspectionJournalHeaderRepository;
import cn.com.huzo.repository.trade.InspectionJournalLinesCertificationHeaderRepository;
import cn.com.huzo.repository.trade.InspectionJournalLinesMaterialRepository;
import cn.com.huzo.repository.trade.InspectionJournalLinesNoOfStyleRepository;
import cn.com.huzo.service.trade.InspectionJournalHeaderManager;
import huzofw.common.util.annotations.WriteLog;
import huzofw.model.entities.paginator.Paginator;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 化检日记账管理服务对象类实现。
 *
 * @author 范学春
 */
@Service("InspectionJournalHeaderManager")
public class InspectionJournalHeaderManagerImpl extends GenericManagerImpl<InspectionJournalHeaderEntity, Long>
        implements InspectionJournalHeaderManager {

    @Resource(name = "InspectionJournalHeaderRepository")
    private InspectionJournalHeaderRepository _dao;

    @Resource(name = "InspectionJournalLinesNoOfStyleRepository")
    private InspectionJournalLinesNoOfStyleRepository _daoLineNoOfStyle;

    @Resource(name = "InspectionJournalLinesMaterialRepository")
    private InspectionJournalLinesMaterialRepository _linesMaterialDao;

    @Resource(name = "InspectionJournalLinesCertificationHeaderRepository")
    private InspectionJournalLinesCertificationHeaderRepository _daoLinesCertification;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Map<String, Object> fetchByHeaderKey(Long headerKey) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        rtnValue.put("HeaderEntity", _dao.fetchByKey(headerKey));
        rtnValue.put("LinesNoOfStyleList", _daoLineNoOfStyle.fetchByHeaderKey(headerKey));
        rtnValue.put("LinesCertificationList", _daoLinesCertification.fetchByHeaderKey(headerKey));
        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> filters) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) ((Integer) filters.get("Limit")) * (((Integer) filters.get("PageIndex")) - 1);
        filters.put("Offset", offset);

        List<InspectionJournalHeaderEntity> itemList = _dao.fetchForPage(filters);
        long total = _dao.fetchForCount(filters);

        Paginator<InspectionJournalHeaderEntity> paginator = new Paginator<>(itemList, offset, (Integer) filters.get("Limit"), total);
        rtnValue.put("TotalRecordCount", paginator.getTotal());
        rtnValue.put("TotalPages", paginator.getTotalPages());
        rtnValue.put("PageIndex", paginator.getPageIndex());
        rtnValue.put("DataList", paginator.getItemList());
        rtnValue.put("Success", true);

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     */
    @Override
    @WriteLog(modelName = "贸易管理→化检日记账", typeOfOperated = "增加")
    public Long insert(InspectionJournalHeaderEntity instance) throws Exception {
        return _dao.insert(instance);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @WriteLog(modelName = "贸易管理→化检日记账", typeOfOperated = "更新")
    public Integer update(InspectionJournalHeaderEntity instance) throws Exception {
        return _dao.update(instance);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @WriteLog(modelName = "贸易管理→化检日记账", typeOfOperated = "删除")
    public Integer delete(InspectionJournalHeaderEntity instance) throws Exception {
        return _dao.delete(instance.getHeaderId());
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public String fetchNewNoOfBill() throws Exception {
        return _dao.fetchNewNoOfBill();
    }
}
